#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import cgi
import json
import db_conf
import time

con = None

try:
    #print time.asctime( time.localtime(time.time()) )	
    qform = cgi.FieldStorage()
    search_input = qform.getvalue('search_input')	
    config = db_conf.getConfig('metabvis.conf')
    con = psycopg2.connect(host=db_conf.getDBHost(config), database=db_conf.getDBName(config), port=db_conf.getDBPort(config), user=db_conf.getUser(config), password=db_conf.getPassword(config)) 
    cur = con.cursor()
    select_experiments = "select compound_id, compound_name from compound where archived = 0 and compound_name is not null and length(compound_name) > 3"
    cur.execute(select_experiments)          
    rows = cur.fetchall()    
    print "Content-type: text/html;charset=utf-8\r\n"
    pa = {}
    #parent = {}
    for row in rows:
	pi = []
	pa[row[0]] = row[1]
    print json.dumps(pa)
except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1)    
finally:
    if con:
        con.close()
